package net.oschina.app.ui;

import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import net.oschina.app.Myapp;
import net.oschina.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabhost)
    FragmentTabHost tabhost;
    @BindView(R.id.iv_quick_option)
    ImageView ivQuickOption;
    @BindView(R.id.naviga_view)
    NavigationView navigaView;
    @BindView(R.id.dl_drawer)
    DrawerLayout dlDrawer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initTabHost();
        initEvent();
    }



    private void initTabHost() {
            //初始化底部标签
        tabhost.setup(this,getSupportFragmentManager(),R.id.fl_main_framelayou);
        //判断版本是否大于10  是就不显示标签之间 的分割线  （原因是太丑）
        if(android.os.Build.VERSION.SDK_INT>10){
            //分割线其实就是一个view  通过tabhost获得这个view并设置不可见
            TabWidget widget = tabhost.getTabWidget();
            widget.setCurrentTab(0);
        }

        //接下来往tabhost中添加标签
        addTab();

    }

    private void addTab() {
        MainTab[] mainTab = MainTab.values();
        for (int i = 0; i <mainTab.length ; i++) {
            MainTab mainTab1 = mainTab[i];
            TabHost.TabSpec tab = tabhost.newTabSpec(this.getResources().getString(mainTab1.getResName()));
            // 初始化标签 ↓
            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(
                    mainTab1.getResIcon());
            //设置TextView上下左右的绘图
            title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);
            if (i == 2) {
                //当是正中间的标签，则隐藏 ，用于显示ImageView 弹出对话框的控件
                indicator.setVisibility(View.INVISIBLE);
            }
            title.setText(getString(mainTab1.getResName()));
            //设置标签的指示器，是一个View
            tab.setIndicator(indicator);

            //2. 设置要传递给Fragment的参数
            Bundle bundle = new Bundle();
            bundle.putString("key",
                    "content: " + getString(mainTab1.getResName()));
            // 3. 把新的标签添加到TabHost中
            tabhost.addTab(tab, mainTab1.getClz(), bundle);

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void initView() {
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,dlDrawer,toolbar,R.string.open,R.string.close);
        toggle.syncState();
        dlDrawer.addDrawerListener(toggle);

    }
    private void initEvent() {
        ivQuickOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Myapp.getContext(),"666",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
